package cn.schoolwow.workflow.module.task.action.flow.complete.listener;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.module.common.domain.QuickWorkFlowOption;

import java.util.List;

public class ExecuteAfterTaskCreateListenerFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickWorkFlowOption quickWorkFlowOption = flowContext.checkInstanceData(QuickWorkFlowOption.class);

        if(null==quickWorkFlowOption.workFlowTaskListener){
            return;
        }
        //如果不是中间节点，则没有任务创建
        if(flowContext.containInstanceKey(WorkFlowTaskResponse.class)){
            WorkFlowTaskResponse workFlowTaskResponse = flowContext.checkInstanceData(WorkFlowTaskResponse.class);
            quickWorkFlowOption.workFlowTaskListener.afterTaskCreate(workFlowTaskResponse);
        }
        if(flowContext.containKey("workFlowTaskResponseList")){
            List<WorkFlowTaskResponse> workFlowTaskResponseList = flowContext.getData("workFlowTaskResponseList", List.class);
            for(WorkFlowTaskResponse workFlowTaskResponse:workFlowTaskResponseList){
                quickWorkFlowOption.workFlowTaskListener.afterTaskCreate(workFlowTaskResponse);
            }
        }
    }

    @Override
    public String name() {
        return "执行工作流任务创建之后事件";
    }
}
